﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPAIS.Updater
{
    /// <summary>
    /// Enumerates state of updating.
    /// </summary>
    public enum UpdateState
    { 
        Ready,
        Updating,
        Failed,
        Cancelled,
        Completed
    }

    /// <summary>
    /// Used to break out when updating cancelled.
    /// </summary>
    public class CancelUpdatingException : Exception
    {         
    }

    /// <summary>
    /// Represents an abstract updater. It can be used to cancel updating, update
    /// progress or status.
    /// </summary>
    /// <exception cref="CancelUpdatingException">Thrown when cancelling updating</exception>
    public abstract class Updater
    {
        #region Variables in Updater
        protected string message;
        private UpdateState state;
        #endregion

        /// <summary>
        /// Default constructor.
        /// </summary>
        public Updater()
        {
            message = "";
            state = UpdateState.Ready;
        }

        /// <summary>
        /// Gets message of updating.
        /// </summary>
        public string Message
        {
            get { return message; }
        }

        /// <summary>
        /// Gets state of updating.
        /// </summary>
        public UpdateState State
        {
            get { return state; }
        }

        /// <summary>
        /// Begins the updating.
        /// </summary>
        public void Begin()
        {
            state = UpdateState.Updating;
            message = "Initializing";
            OnBegin();
        }
        protected virtual void OnBegin() { }

        /// <summary>
        /// Cancels the updating if it is on updating, it can only be called when 
        /// updating some thing.
        /// </summary>
        /// <exception cref="CancelUpdatingException">Thrown when cancelling updating</exception>
        protected void Cancel()
        {
            if (state == UpdateState.Updating)
            {
                message = "Cancelled";
                state = UpdateState.Cancelled;
                OnCancel();
                throw new CancelUpdatingException();
            }
        }
        protected virtual void OnCancel() { }

        /// <summary>
        /// Sets the updating failed if an error occurred.
        /// </summary>
        /// <param name="failMessage">Message of failure</param>
        public void Fail(string failMessage)
        {
            if (state == UpdateState.Updating)
            {
                message = failMessage;
                state = UpdateState.Failed;
                OnFail(failMessage);
            }
        }
        protected virtual void OnFail(string failMessage) { }

        /// <summary>
        /// Sets the updating state completed if it is updating now.
        /// </summary>
        public void Complete()
        {
            if (state == UpdateState.Updating)
            {
                state = UpdateState.Completed;
                message = "Completed";                
                OnComplete();
            }
        }
        protected virtual void OnComplete() { }
    }
}
